package com.jd.tuning.jvm.lock.reen;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 *
 */
public class ReentrantReadWriteLockTest1 {
    private Object data;
    //缓冲是否有效
    private volatile  boolean cacheValid;
    private ReentrantReadWriteLock rwl=new ReentrantReadWriteLock();

    public  void processCachedData(){
        rwl.readLock().lock();
        //如果缓冲无效，更新cache，否则直接使用data
        if(!cacheValid){
            //获取写锁前，必须释放读锁
            rwl.readLock().unlock();
            rwl.writeLock().lock();
            if(!cacheValid){
                //更新数据
                data=new Object();
                cacheValid=true;
            }
            //锁降级，在释放写锁前获取读锁
            rwl.readLock().lock();
            //释放写锁，依然持有读锁
            rwl.writeLock().unlock();
        }
        //使用缓冲
        //.....
        //释放读锁
        rwl.readLock().unlock();
    }


    public static void main(String[] args) throws InterruptedException {

    }
}
